<?php
declare (strict_types=1);

namespace app\common\model;

use think\Collection;
use think\facade\Cache;

class Flag extends CommonBase
{
    private const  _CACHE_KEY = '%s_flags';

    public function getAllFlag(array $where = []): array
    {
        $cacheKey = sprintf(self::_CACHE_KEY, $this->connection);
        $flags    = Cache::get($cacheKey);
        if (empty($flags)) {
            $flags = $this->where(['is_delete' => config('status.mysql.table_normal')])->order('id asc')->select()->toArray();
            Cache::set($cacheKey, $flags,self::CACHE_EXPIRE_TIME);
        }

        $res = [];
        foreach ($flags as $row) {
            $f = true;
            foreach ($where as $key => $value) {
                if (is_array($value) && count($value) == 3 && $value[1] == 'in' && is_array($value[2])) {
                    if (!in_array($row[$value[0]], $value[2])) {
                        $f = false;
                        break;
                    }
                } else if ($row[$key] != $value) {
                    $f = false;
                    break;
                }
            }

            if ($f) {
                $res[$row['code']] = $row;
            }
        }

        return $res;
    }

    public function getFlagList(int $page = 1, int $limit = 10, array $where = []): Collection
    {
        $where['is_delete'] = config('status.mysql.table_normal');
        $offset             = ($page - 1) * $limit;
        return $this->where($where)->limit($offset, $limit)->select();
    }

    public function getFlagCount(array $where = []): int
    {
        $where['is_delete'] = config('status.mysql.table_normal');
        return $this->where($where)->count();
    }

    public function findFlagById(int $id)
    {
        return $this->where(['id' => $id, 'is_delete' => config('status.mysql.table_normal')])->find();
    }

    public function findFlagByCode(string $code)
    {
        return $this->where(['code' => $code, 'is_delete' => config('status.mysql.table_normal')])->find();
    }

    public function addFlag(array $data): bool
    {
        unset($data['website_code']);
        $data['is_delete'] = config('status.mysql.table_normal');

        $flagObj = $this->where(['is_delete' => config('status.mysql.table_delete'), 'code' => $data['code']])->find();
        if (!empty($flagObj)) {
            $flag = $flagObj->toArray();
            return (boolean)$this->where(['id' => $flag['id']])->update([
                'name'       => $data['name'],
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
                'is_delete'  => config('status.mysql.table_normal')]);
        }


        $res = $this->save($data);
        Cache::delete(sprintf(self::_CACHE_KEY, $this->connection));
        return $res;
    }

    public function updateFlag(int $id, array $data): bool
    {
        $data['updated_at'] = date('Y-m-d H:i:s');
        $res                = (boolean)$this->where(['id' => $id])->save($data);
        Cache::delete(sprintf(self::_CACHE_KEY, $this->connection));
        return $res;
    }

    public function deleteFlag(int $id): bool
    {
        $res = $this->where(['id' => $id])->save(['is_delete' => config('status.mysql.table_delete')]);
        Cache::delete(sprintf(self::_CACHE_KEY, $this->connection));
        return $res;
    }
}
